package com.homegoods.login.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.homegoods.login.KGetVerHelper;
import com.homegoods.login.R;
import com.homegoods.login.databinding.LoginRegistActivityBindingImpl;
import com.homegoods.login.presenter.RegisterPresenter;
import com.homegoods.login.view.RegisterView;
import com.homegoods.aroutercomment.ARouterPath;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.KeyBoardUtils;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import java.util.Objects;

/**
 * @author QiQi
 * @ClassName: RegisterActivity
 * @date 2018/9/13
 * @description
 * 注册
 */
@Route(path = ARouterPath.PATH_REGISTER)
public class RegisterActivity extends MyBaseFragmentActivity<LoginRegistActivityBindingImpl, RegisterPresenter> implements RegisterView {

    KGetVerHelper kGetVerHelper;

    @Override
    protected int getLayout() {
        return R.layout.login_regist_activity;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        RxTextView.afterTextChangeEvents(mBinding.pwd)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.registerButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.phoneNum.getText())
                                && StringUtil.isNotEmpty(mBinding.inputVer.getText())
                ));

        RxTextView.afterTextChangeEvents(mBinding.phoneNum)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.registerButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.pwd.getText())
                                && StringUtil.isNotEmpty(mBinding.inputVer.getText())
                ));

        RxTextView.afterTextChangeEvents(mBinding.inputVer)
                .compose(bindToLifecycle())
                .subscribe(afterTextChangeEvent -> mBinding.registerButton.setEnabled(
                        Objects.requireNonNull(afterTextChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.pwd.getText())
                                && StringUtil.isNotEmpty(mBinding.phoneNum.getText())
                ));

        mBinding.setRegister(mPresenter);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (kGetVerHelper != null) {
            kGetVerHelper.restart();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (kGetVerHelper != null) {
            kGetVerHelper.stop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (kGetVerHelper != null) {
            kGetVerHelper.cancel();
            kGetVerHelper = null;
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setTitle("注册");
    }

    @Override
    protected RegisterPresenter getPresenter() {
        kGetVerHelper = new KGetVerHelper(this, mBinding.getVer);
        return new RegisterPresenter(this, kGetVerHelper);
    }

    @Override
    public void hideKeyBoard() {
        KeyBoardUtils.hideKeyBoard(this);
    }

    @Override
    public void showMainActivity() {
        ToastUtil.showToast(R.string.string_reg_suc);
        Intent intent = new Intent();
        intent.putExtra("phoneNum", mBinding.phoneNum.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }

}
